package com.yq.socket1.day1.v2;

import com.sun.javafx.image.impl.ByteRgb;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author 3307235355@qq.com
 * @description
 * @since 2022/07/01 09:58
 */
public class Server {
    public static void main(String[] args) throws IOException {

        //    serversocket
        ServerSocket serverSocket = new ServerSocket(8888);
        ExecutorService pool = Executors.newFixedThreadPool(2);
        while (true) {
            Socket socket = serverSocket.accept();
            // new  Thread(new ConnectTask(socket)).start();

            //    线程池
            pool.submit(new ConnectTask(socket));
        }
    }

}

class ConnectTask implements Runnable {
    Socket socket;

    public ConnectTask(Socket socket) {
        this.socket = socket;
    }
    @Override
    public void run() {
        //读取数据
        while (true) {
            InputStream in = null;
            try {
                in = socket.getInputStream();
                byte[] bytes = new byte[1024];
                int read = in.read(bytes);
                String s = new String(bytes, 0, read);
                System.out.println(socket.getInetAddress() + "---" + socket.getPort()+""+s);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }
}
